﻿window.zsmm = {};
zsmm.isValidated = true;

zsmm.Toast = {
    isopen: true,
    ins: null,
    intval: 0,
    New: function (msg, remain) {
        var that = this;
        if (this.isopen && this.ins) {
            this.clear();
        }
        this.instance(msg);
        remain || (remain = 3);
        this.intval = setTimeout(function () {
            that.ins.css('opacity', 0);
            that.clear();
        }, remain * 1000);
    },
    instance: function (msg) {
        this.ins = $("<div id='global-toast'></div>");
        this.ins.appendTo('body');
        this.ins.html("<p>" + msg + "</p>");
        this.ins.show();
        var that = this;
    },
    clear: function () {
        this.ins.remove();
        this.ins = null;
        clearTimeout(this.intval);
    }
};
zsmm.Popup = {
    isopen: true,
    ins: null,
    New: function (obj, close) {
        var that = this;
        if (this.isopen && this.ins) {
            that.clear();
        }
        this.instance(obj, close);
    },
    instance: function (obj, close) {
        if (!(obj.length > 0)) return false;
        var that = this;
        this.ins = $('<div class="popup-wrapper"></div>');
        this.ins.appendTo('body');
        obj = obj.clone().removeClass("hide").prop("outerHTML");
        this.ins.html('<div class="popup-layout vertical">' + obj + '</div>');
        this.ins.show();
        that.init(close);
    },
    init: function (close) {
        var that = this;
        var toClose = (typeof (close) == "undefined") ? 1 : close;
        if (toClose == 0) return false;
        this.ins.find(".popup-layout").click(function (e) {
            e.preventDefault();
            that.clear();
        });
        this.ins.find(".popup-layout>div").click(function (e) {
            e.preventDefault();
            e.stopPropagation();
        });
        if (this.ins.find(".close").length > 0) {
            this.ins.find(".close").click(function (e) {
                e.preventDefault();
                that.clear();
            });
        }
        document.addEventListener('touchmove', this.scroll, false);
    },
    clear: function () {
        this.ins.remove();
        this.ins = null;
        document.removeEventListener('touchmove', this.scroll, false);
    },
    scroll: function () {
        var that = this;
        if (!($(that).is(":hidden"))) {
            event.preventDefault();
        }
    }
};
zsmm.Dropdown = {
    isopen: true,
    ins: null,
    New: function (obj) {
        var that = this;
        if (this.isopen && this.ins) {
            this.clear(obj);
        }
        obj.parent().css("position", "relative").append("<div class='dropdown-mask'></div>");
        obj.siblings(".dropdown-mask").click(function (e) {
            e.preventDefault();
            that.instance(obj);
        });
    },
    instance: function (obj) {
        if (obj.find("option").length == 1 && obj.find("option").val() == "") {
            return false;
        }
        var options = "";
        obj.prop('disabled', true);
        this.ins = $('<modal class="global-dropdown vertical"></modal>');
        this.ins.appendTo('body');
        this.ins.siblings("div").addClass("disableClick");
        $("body").addClass('noScroll');
        obj.find("option").each(function (index, element) {
            if ($(this).val() && $(this).val() != "") {
                var className = "";
                if ($(this).val() == obj.val()) {
                    className = "active";
                }
                options = options + "<li data-value='" + $(this).val() + "' class='" + className + "'><span></span>" + $(this).text() + "</li>";
            }
        });
        this.ins.html("<dialog><content><ul>" + options + "</ul></content></dialog>");
        this.ins.show();
        this.init(obj);
        this.selectChild(obj);
    },
    clear: function (obj) {
        $("body").removeClass('noScroll');
        obj.prop('disabled', false);
        this.ins.siblings("div").removeClass("disableClick");
        this.ins.remove();
        this.ins = null;
        window.ontouchstart = function (e) {
        };
        window.ontouchend = function (e) {
        };
        window.ontouchcancel = function (e) {
        };
    },
    selectChild: function (obj) {
        var that = this;
        this.ins.find("li").on('click', function (e) {
            e.preventDefault();
            $(this).siblings("li").removeClass("active");
            $(this).addClass("active");
            obj.val($(this).data("value"));
            setTimeout(function () {
                that.clear(obj);
            }, 200);
        });
    },
    init: function (obj) {
        var that = this;
        var dialog = document.querySelector('dialog');
        var touchX, touchY;
        window.ontouchstart = function (e) {
            if (!document.body.classList.contains('noScroll')) return;
            var touch = e.changedTouches[0];
            touchX = touch.screenX;
            touchY = touch.screenY;
            if (!dialog.contains(e.target)) e.preventDefault();
            if (dialog.scrollTop === 0) e.preventDefault();
            else if (dialog.scrollTop === dialog.scrollHeight - dialog.clientHeight) e.preventDefault();
        };

        window.ontouchend = function (e) {
            var touch = e.changedTouches[0];
            if (Math.abs(touch.screenY - touchY) > 10 || Math.abs(touch.screenX - touchX) > 10) return;
            if (!dialog.contains(e.target)) that.clear(obj);
            touchX = void 0;
            touchY = void 0;
        };

        window.ontouchcancel = function (e) {
            touchX = void 0;
            touchY = void 0;
        };

        var padding = 1;
        requestAnimationFrame(function frame() {
            var min = 0,
				max = dialog.scrollHeight - dialog.clientHeight;
            var val = dialog.scrollTop;
            if (val === min) {
                dialog.scrollTop += padding;
            } else if (val === max) {
                dialog.scrollTop -= padding;
            }
            requestAnimationFrame(frame);
        });
    }
};


//触发点击时添加点击样式
document.documentElement.ontouchstart = function (e) {
    if ($(e.target).attr('data-onhover') && $(e.target).data('onhover')) {
        $(e.target).addClass($(e.target).data('onhover'));
    }
}
document.documentElement.ontouchend = function () {
    $('[data-onhover]').each(function () {
        $(this).removeClass($(this).data('onhover'));
    });
}

$("body").delegate('[data-href]', 'click', function () {//页面跳转
    window.location.href = $(this).data('href');
});

//验证
function validateInput(obj) {
    if ($.trim($("#" + obj).val()) == "" && zsmm.isValidated) {
        zsmm.Toast.New($("#" + obj).data("message"));
        zsmm.isValidated = false;
    }
}
function validatePhone(obj) {
    var phone = $.trim($("#" + obj).eq(0).val());
    if (!/^1[1-9]\d{9}$/.test(phone) && zsmm.isValidated) {
        zsmm.Toast.New('手机格式不正确');
        zsmm.isValidated = false;
    }
}
function validateSelect(obj) {
    if ($.trim($("#" + obj).find("option").not(function () { return !this.selected }).val()) == "" && zsmm.isValidated) {
        zsmm.Toast.New($("#" + obj).data("message"));
        zsmm.isValidated = false;
    }
}
function validateRadio(obj) {
    if ($.trim($("#" + obj).find("input[type='radio']:checked").val()) == "" && zsmm.isValidated) {
        zsmm.Toast.New($("#" + obj).data("message"));
        zsmm.isValidated = false;
    }
}
function validateInputFile(obj) {
    if (($("#" + obj).find("input").length < 1) && zsmm.isValidated) {
        zsmm.Toast.New($("#" + obj).data("message"));
        zsmm.isValidated = false;
    }
}
function validateIdCard(obj) {
    var idCard = $.trim($("#" + obj).val());
    if (zsmm.isValidated) {
        //15位和18位身份证号码的正则表达式
        var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;

        //如果通过该验证，说明身份证格式正确，但准确性还需计算
        if (regIdCard.test(idCard)) {
            if (idCard.length == 18) {
                var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //将前17位加权因子保存在数组里
                var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); //这是除以11后，可能产生的11位余数、验证码，也保存成数组
                var idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和
                for (var i = 0; i < 17; i++) {
                    idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
                }

                var idCardMod = idCardWiSum % 11;//计算出校验码所在数组的位置
                var idCardLast = idCard.substring(17);//得到最后一位身份证号码

                //如果等于2，则说明校验码是10，身份证号码最后一位应该是X
                if (idCardMod == 2) {
                    if (idCardLast == "X" || idCardLast == "x") {

                    } else {
                        zsmm.Toast.New('身份证号码错误！');
                        zsmm.isValidated = false;
                    }
                } else {
                    //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
                    if (idCardLast == idCardY[idCardMod]) {

                    } else {
                        zsmm.Toast.New('身份证号码错误！');
                        zsmm.isValidated = false;
                    }
                }
            }
        } else {
            zsmm.Toast.New('身份证格式不正确!');
            zsmm.isValidated = false;
        }
    }
}
function validateHMPass(obj) {
    var passort = $.trim($("#" + obj).val());
    var Expression = /^[HMhm]{1}([0-9]{10}|[0-9]{8})$/;
    if (!Expression.test(passort)) {
        zsmm.Toast.New('港澳通行证格式不正确!');
        zsmm.isValidated = false;
    }
}
function validateTPass(obj) {
    var passort = $.trim($("#" + obj).val());
    var Expression1 = /^[0-9]{8}$/;
    var Expression2 = /^[0-9]{10}$/;
    if (!(Expression1.test(passort) || Expression2.test(passort))) {
        zsmm.Toast.New('台湾通行证格式不正确!');
        zsmm.isValidated = false;
    }
}
function validatePassport(obj) {
    var passort = $.trim($("#" + obj).val());
    var Expression1 = /^[a-zA-Z]{5,17}$/;
    var Expression2 = /^[a-zA-Z0-9]{5,17}$/;
    if (!(Expression1.test(passort) || Expression2.test(passort))) {
        zsmm.Toast.New('护照格式不正确!');
        zsmm.isValidated = false;
    }
}
function validateCardNumber(obj, type) {
    switch (type) {
        case "1":
            validateIdCard(obj);
            break;
        case "2":
            validateHMPass(obj);
            break;
        case "3":
            validateTPass(obj);
            break;
        case "4":
            validatePassport(obj);
            break;
    }
}
$(function () {
    if ($("#showMap").length > 0) {
        $("#showMap").click(function (e) {
            e.preventDefault();
            $(".popup-map").show();
            $(".popup-map .pinch-zoom").css("width", "100%");
        });
    }
    if ($(".popup-map").length > 0) {
        $(".popup-map").click(function (e) {
            e.preventDefault();
            $(".popup-map").hide();
        });
    }
    $('div.pinch-zoom').each(function (index, element) {
        new RTP.PinchZoom($(this), {});
    });
});